/**
 * @param {number[][]} segments
 * @return {number[][]}
 */
 var splitPainting = function(segments) {
    segments.sort((a,b) => {
        if(a[0] === b[0]){
            return a[1]-b[1]
        }
        return a[0] - b[0]
    })
    
    const temp = segments.map(item => item[1])
    const left = segments[0][0], right = Math.max(...temp)
    const total = new Array(right-left+1).fill('0-')
    const n = total.length
    for(const [x, y, color] of segments){
        for(let k = x-left; k<y-left; k++){
            const arr = total[k].split('-')
            const t = Number(arr[0])+color
            total[k] = t+`-${arr[1]}${color}`
        }
    }
    const ret = []
    // for(let i = left; i<=right; i++){
    //     for(let j = left+1; j<=right; j++){
    //         const cur = `${i}-${j}`
    //         if(map.has(cur)){
    //             ret.push([i, j, map.get(cur)])
    //         }
    //     }
    // }
    // console.log(left, right, total)
    for(let i = 1, pre = 0; i<n; i++){
        const preArr = total[pre].split('-'), now = total[i].split('-')
        if(preArr[0] !== now[0] && preArr[1] !== now[1]){
            ret.push([pre+left, i+left, Number(preArr[0])])
            pre = i
        }
    }
    return ret
};